<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="部门ID" prop="deptId">-->
<!--        <el-input-->
<!--          v-model="queryParams.deptId"-->
<!--          placeholder="请输入部门ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="工位ID" prop="stationId">-->
<!--        <el-input-->
<!--          v-model="queryParams.stationId"-->
<!--          placeholder="请输入工位ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="组别" prop="groupId">
        <el-select v-model="queryParams.groupId" placeholder="请选择组别" clearable>
          <el-option v-for="group in groups" :key="group.id" :label="group.groupName"
                     :value="group.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工种类型" prop="workerType">
        <el-select v-model="queryParams.workerType" placeholder="请选择工种类型" clearable>
          <el-option v-for="dict in workerTypes" :key="dict.dictValue" :label="dict.dictLabel"
                     :value="dict.dictValue" />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="人员状态" prop="peopleState">-->
<!--        <el-select v-model="queryParams.peopleState" placeholder="请选择人员状态" clearable>-->
<!--          <el-option v-for="dict in peopleStateOptions" :key="dict.dictValue" :label="dict.dictLabel"-->
<!--                     :value="dict.dictValue" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:peopleDispatch:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:peopleDispatch:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:peopleDispatch:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:peopleDispatch:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="peopleDispatchList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" v-if="showIdColumn" />
<!--      <el-table-column label="部门ID" align="center" prop="deptId" />-->
      <el-table-column label="序号" type="index" :index="indexMethod"></el-table-column>
      <el-table-column label="工位" align="center" prop="stationId" :formatter="stationFormatter" width="240" />
      <el-table-column label="组别" align="center" prop="groupId" :formatter="groupFormatter" />
      <el-table-column label="人员" align="center" prop="employeeId" :formatter="employeeFormatter" />
      <el-table-column label="工种" align="center" prop="workerType"  :formatter="workerTypeFormatter"  />
      <el-table-column label="人员状态" align="center" prop="peopleState"  :formatter="peopleStateFormatter" />
      <el-table-column label="备注" align="center" prop="remark" />
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['system:peopleDispatch:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:peopleDispatch:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人员调度对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="部门ID" prop="deptId">-->
<!--          <el-input v-model="form.deptId" placeholder="请输入部门ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="工位ID" prop="stationId">
          <el-input v-model="form.stationId" placeholder="请输入工位ID" />
        </el-form-item>
        <el-form-item label="组别id" prop="groupId">
          <el-input v-model="form.groupId" placeholder="请输入组别id" />
        </el-form-item>
        <el-form-item label="人员id" prop="employeeId">
          <el-input v-model="form.employeeId" placeholder="请输入人员id" />
        </el-form-item>
        <el-form-item label="人员状态" prop="peopleState">
          <el-input v-model="form.peopleState" placeholder="请输入人员状态" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import { listGroup } from '@/api/zndd/rest/group'
import { listEmployee } from '@/api/zndd/rest/employee'
import { listStation } from '@/api/zndd/process/station'
import {
  addProcessdetails, delProcessdetails,
  getProcessdetails,
  listProcessdetails,
  updateProcessdetails
} from '@/api/zndd/process/processdetails'

export default {
  name: "PeopleDispatch",
  data() {
    return {
      showIdColumn: false,
      peopleStateOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员调度表格数据
      peopleDispatchList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        stationId: null,
        groupId: null,
        employeeId: null,
        workerType: null,
        peopleState: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        peopleState: [
          { required: true, message: "人员状态不能为空", trigger: "blur" }
        ],
      },
      workerTypes:[],
      groups:[],
      employeeList:[],
      stationList:[],
    };
  },
  created() {
    this.getList();
    //获取字典表中工种类型
    this.getDicts("zndd_job_type").then(response => {
      this.workerTypes = response.data;
    });
    //获取字典表中人员状态
    this.getDicts("station_state").then(response => {
      this.peopleStateOptions = response.data;
    });
    var param = {pageNum:1,pageSize:1000}
    //获取组别数据
    listGroup(param).then(response => {
      this.groups = response.rows;
    });
    listEmployee(param).then(response=>{
      this.employeeList = response.rows;
    });
    listStation(param).then(response=>{
      this.stationList = response.rows;
    })
  },
  methods: {
    //格式化人员状态
    peopleStateFormatter(row){
      var wt = null;
      this.peopleStateOptions.forEach(people => {
        if (people.dictValue == row.peopleState) {
          wt = people;
        }
      });
      if(wt == null){
        return "";
      }else{
        return wt.dictLabel;
      }
    },
    //格式化工位
    stationFormatter(row){
      var lt = null;
      this.stationList.forEach(station => {
        if(station.id == row.stationId){
          lt = station;
        }
      });
      if(lt == null){
        return "";
      }else{
        return lt.stationNo+"-"+lt.stationName;
      }

    },
    //格式化组别
    groupFormatter(row) {
      var gp = null;
      this.groups.forEach(group => {
        if (group.id == row.groupId) {
          gp = group;
        }
      });
      if(gp == null){
        return "";
      }else{
        return gp.groupName;
      }

    },
    //格式化人员
    employeeFormatter(row){
      var lt = null;
      this.employeeList.forEach(employee => {
        if(employee.id == row.employeeId){
          lt = employee;
        }
      });
      if(lt == null){
        return "";
      }else{
        return lt.employeeNo+" - "+lt.employeeName;
      }

    },
    //格式化工序类型
    workerTypeFormatter(row) {
      var wt = null;
      this.workerTypes.forEach(workerType => {
        if (workerType.dictValue == row.workerType) {
          wt = workerType;
        }
      });
      if(wt == null){
        return "";
      }else{
        return wt.dictLabel;
      }

    },
    /** 序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if (pageNum !== -1 && pageNum !== 0) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return index + 1;
      }
    },
    /** 查询人员调度列表 */
    getList() {
      this.loading = true;
      this.queryParams.peopleState="1";
      listProcessdetails(this.queryParams).then(response => {
        this.peopleDispatchList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        stationId: null,
        groupId: null,
        employeeId: null,
        workerType: null,
        peopleState: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人员调度";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProcessdetails(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人员调度";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProcessdetails(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProcessdetails(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除人员调度编号为"' + ids + '"的数据项？').then(function() {
        return delProcessdetails(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/peopleDispatch/export', {
        ...this.queryParams
      }, `peopleDispatch_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
